package com.iss.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;

@WebListener()
public class ServletRequestListener1 implements ServletRequestListener {
   public ServletRequestListener1(){

   }

    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {

    }

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
        servletRequestEvent.getServletRequest().setAttribute("testAttribute","tst");

        ServletContext application = servletRequestEvent.getServletContext();
        Integer count = (Integer) application.getAttribute("count");

        if (count == null) {
            count = 0;
        } else {
            count++;
        }

        application.setAttribute("count", count);
         String  servletName=  servletRequestEvent.getServletRequest().getServerName();

        System.out.println(servletName + " is running...");
        System.out.println("current user visited " + count +  " servletRequests");
    }
}
